/* 检查变位词： 相同字母的重新排列 */
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#define N 20
int read_line(char str[], int n);
bool are_anagrams(const char *word1, const char *word2);
int main(void)
{
    char word1[N+1], word2[N+1];
    printf("Enter first word: ");
    read_line(word1, N+1);
    printf("Enter second word: ");
    read_line(word2, N+1);
    printf("The words are%s anagrams.\n", are_anagrams(word1, word2) ? "" : " not");
    return 0;
}
int read_line(char str[], int n)
{
    int ch, i = 0;
    while((ch = getchar()) != '\n' && ch != EOF)
        if (i < n)
            str[i++] = ch;
    str[i] = '\0';
    return i;
}
bool are_anagrams(const char *word1, const char *word2) {
    int letters[26] = {0};
    for (const char *p = word1; *p; p++)
        letters[tolower(*p) - 'a']++;
    for (const char *p = word2; *p; p++)
        letters[tolower(*p) - 'a']--;
    for (int i = 0; i < 26; i++)
        if (letters[i] != 0) 
            return false;
    return true;
}